"""
通知系统模型
"""
from django.db import models
from django.conf import settings


class NotificationTemplate(models.Model):
    """通知模板"""
    
    TYPE_CHOICES = [
        ('system', '系统通知'),
        ('resume', '简历通知'),
        ('interview', '面试通知'),
        ('job', '岗位通知'),
        ('email', '邮件通知'),
    ]
    
    name = models.CharField(max_length=100, unique=True, verbose_name='模板名称')
    notification_type = models.CharField(max_length=20, choices=TYPE_CHOICES, verbose_name='通知类型')
    title_template = models.CharField(max_length=200, verbose_name='标题模板')
    content_template = models.TextField(verbose_name='内容模板')
    is_active = models.BooleanField(default=True, verbose_name='是否启用')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        db_table = 'notifications_template'
        verbose_name = '通知模板'
        verbose_name_plural = '通知模板'
        ordering = ['notification_type', 'name']
        
    def __str__(self):
        return self.name


class Notification(models.Model):
    """通知记录"""
    
    TYPE_CHOICES = [
        ('system', '系统通知'),
        ('resume', '简历通知'),
        ('interview', '面试通知'),
        ('job', '岗位通知'),
    ]
    
    PRIORITY_CHOICES = [
        ('low', '低'),
        ('normal', '普通'),
        ('high', '高'),
        ('urgent', '紧急'),
    ]
    
    recipient = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name='接收人')
    sender = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='sent_notifications',
        verbose_name='发送人'
    )
    
    notification_type = models.CharField(max_length=20, choices=TYPE_CHOICES, verbose_name='通知类型')
    title = models.CharField(max_length=200, verbose_name='通知标题')
    content = models.TextField(verbose_name='通知内容')
    priority = models.CharField(max_length=10, choices=PRIORITY_CHOICES, default='normal', verbose_name='优先级')
    
    # 关联对象
    related_object_type = models.CharField(max_length=50, blank=True, null=True, verbose_name='关联对象类型')
    related_object_id = models.PositiveIntegerField(blank=True, null=True, verbose_name='关联对象ID')
    
    # 状态
    is_read = models.BooleanField(default=False, verbose_name='是否已读')
    read_at = models.DateTimeField(blank=True, null=True, verbose_name='阅读时间')
    
    # 发送状态
    is_sent = models.BooleanField(default=False, verbose_name='是否已发送')
    sent_at = models.DateTimeField(blank=True, null=True, verbose_name='发送时间')
    
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    
    class Meta:
        db_table = 'notifications_notification'
        verbose_name = '通知'
        verbose_name_plural = '通知'
        ordering = ['-created_at']
        
    def __str__(self):
        return f"{self.recipient.username} - {self.title}"


class EmailNotification(models.Model):
    """邮件通知"""
    
    STATUS_CHOICES = [
        ('pending', '待发送'),
        ('sent', '已发送'),
        ('failed', '发送失败'),
    ]
    
    notification = models.OneToOneField(Notification, on_delete=models.CASCADE, verbose_name='关联通知')
    email_subject = models.CharField(max_length=200, verbose_name='邮件主题')
    email_content = models.TextField(verbose_name='邮件内容')
    recipient_email = models.EmailField(verbose_name='收件人邮箱')
    
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending', verbose_name='发送状态')
    error_message = models.TextField(blank=True, null=True, verbose_name='错误信息')
    sent_at = models.DateTimeField(blank=True, null=True, verbose_name='发送时间')
    
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    
    class Meta:
        db_table = 'notifications_email'
        verbose_name = '邮件通知'
        verbose_name_plural = '邮件通知'
        ordering = ['-created_at']
        
    def __str__(self):
        return f"{self.recipient_email} - {self.email_subject}"


class NotificationSettings(models.Model):
    """通知设置"""
    
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name='用户')
    
    # 系统通知设置
    system_notifications = models.BooleanField(default=True, verbose_name='系统通知')
    resume_notifications = models.BooleanField(default=True, verbose_name='简历通知')
    interview_notifications = models.BooleanField(default=True, verbose_name='面试通知')
    job_notifications = models.BooleanField(default=True, verbose_name='岗位通知')
    
    # 邮件通知设置
    email_notifications = models.BooleanField(default=True, verbose_name='邮件通知')
    email_daily_digest = models.BooleanField(default=False, verbose_name='每日邮件摘要')
    
    # 推送设置
    push_notifications = models.BooleanField(default=True, verbose_name='推送通知')
    
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        db_table = 'notifications_settings'
        verbose_name = '通知设置'
        verbose_name_plural = '通知设置'
        
    def __str__(self):
        return f"{self.user.username} - 通知设置"
